#include "dbbusqmlapp.h"
#include <QtGui/QApplication>
#include "QObject"

DbbusQmlApp::DbbusQmlApp(QString qmlFile, QString appName, QObject *parent) : DbusApp(appName,parent)

{
    modeComposite=false;

    qmlViewer = new QmlWindow();
    this->createBackgroundWidget();

    qmlViewer->rootContext()->setContextProperty("appControl", this);


    //forward signal key pressed
    connect(qmlViewer,SIGNAL(qmlViewKeyPressed(int)),this,SLOT(keyEventPressed(int)));
    qDebug()<<"load qmlFile:"<<qmlFile;
    qmlViewer->setSource(QUrl::fromLocalFile(qmlFile));
}

void DbbusQmlApp::createBackgroundWidget(){
    //if(backgroundWidget==0){
        //qDebug()<<"createBackgroundWidget";
        backgroundWidget = new VideoWidget();
        backgroundWidget->setFocusPolicy(Qt::WheelFocus);
        qmlViewer->rootContext()->setContextProperty("backgroundVideo", backgroundWidget);


    //}


}

void DbbusQmlApp::videoBackground(bool activate){
    qDebug()<<"videoBackground set:"<<activate<<" oldState:"<<modeComposite;
    if(activate && !modeComposite)
        this->activeModeComposite();

    if(!activate && modeComposite)
        this->desactiveModeComposite();

    return;
}

void DbbusQmlApp::desactiveModeComposite(){
     qDebug()<<"deconnect";
     disconnect(backgroundWidget,0,0,0);

     qmlViewer->setWindowModality(Qt::NonModal);
     //QFlag f = qmlViewer->windowFlags();
     backgroundWidget->hide();

     QFlag f = !Qt::X11BypassWindowManagerHint;
     qmlViewer->setWindowFlags(f);
     qmlViewer->move(qmlViewer->pos()-pDiff);
     qmlViewer->show();
     modeComposite=false;
     emit(compositeChanged());
}

void DbbusQmlApp::activeModeComposite(){

    QPoint p1 = qmlViewer->frameGeometry().topLeft();
    QPoint p2 = qmlViewer->geometry().topLeft();
    pDiff = p2-p1;

    //qmlViewer->setFocusPolicy(Qt::NoFocus);
    qmlViewer->setWindowFlags(Qt::X11BypassWindowManagerHint);
    //qmlViewer->show();

    //this->createBackgroundWidget();
    backgroundWidget->resize(qmlViewer->size());
    backgroundWidget->move(p1);
    backgroundWidget->show();
    backgroundWidget->activateWindow();
    //backgroundWidget->move(backgroundWidget->pos()+pDiff);

    qDebug()<<"connect";
    connect(backgroundWidget,SIGNAL(resizeEventSglRedirect()),this,SLOT(synchSize()));
    connect(backgroundWidget,SIGNAL(moveEventSglRedirect()),this,SLOT(synchPos()));
    connect(backgroundWidget,SIGNAL(keyPressEventSglRedirect(QKeyEvent*)),qmlViewer,SLOT(onExternKeyPressEvent(QKeyEvent*)));


    modeComposite=true;
    emit(compositeChanged());
}

void DbbusQmlApp::synchPos(){
    if(!modeComposite)
        return;
    //qDebug()<<"synchPos";
    qmlViewer->move(backgroundWidget->geometry().topLeft());
}
void DbbusQmlApp::synchSize(){
    if(!modeComposite)
        return;
    //qDebug()<<"synchSize";
    qmlViewer->resize(backgroundWidget->size());

}
void DbbusQmlApp::showTop(){
    if(!modeComposite)
        return;
    qDebug()<<"showing top";
    qmlViewer->show();


}

void DbbusQmlApp::hideTop(){
    if(!modeComposite)
        return;
    qDebug()<<"hidding top";

    qmlViewer->hide();
}

void DbbusQmlApp::focusAppChanged(QWidget * old, QWidget * now){

    //qDebug()<<"focusAppChanged"<<old<<" vers "<<now << "compos:"<<modeComposite;
    if(!modeComposite)
        return;
    if(now==0){
        qDebug()<<"focusAppChanged=>losed";
        this->hideTop();
    }else{
        qDebug()<<"focusAppChanged=>get";
        this->showTop();
    }


}

void DbbusQmlApp::responseSlot(QString reqkey, QString response){
    qDebug()<<"response from dbus"<<reqkey<<"***"<<response;
    emit(responseMedia(reqkey,response));
}

void DbbusQmlApp::requestMedia(QString reqkey, QString askerApp, QString reqName, QString reqParam) {
    //si on pose une question asynchrone à l appli en cours
    qDebug()<<"requestReceived"<<reqkey<<"==="<<askerApp<<"==="<<reqName<<"==="<<reqParam;
    emit(requestReceived(reqkey,askerApp,reqName,reqParam));

    /*QMetaObject::invokeMethod(qmlViewer->rootObject(), "requestMedia",
        Q_ARG(QVariant, reqkey),
        Q_ARG(QVariant, askerApp),
        Q_ARG(QVariant, reqName),
        Q_ARG(QVariant, reqParam));*/
}

QString DbbusQmlApp::getInfo(QString infokey) {
    /*QVariant returnedValue;
    QMetaObject::invokeMethod(qmlViewer->rootObject(), "getInfo",
         Q_RETURN_ARG(QVariant, returnedValue),
         Q_ARG(QString, infokey));
    return returnedValue;*/
}

void DbbusQmlApp::show(){
    qDebug()<<"showing QML APP";
    if(!modeComposite){
        QRect rect = QApplication::desktop()->screenGeometry(-1);
        qmlViewer->move (rect.center() - qmlViewer->rect().center());
        qmlViewer->show();
    }else{
        backgroundWidget->show();
    }

    qDebug()<<"fin showing QML APP";
    emit(showed());
}

void DbbusQmlApp::keyEventPressed(int keyCode){

    if(Qt::Key_Escape == keyCode){
        qDebug()<<"onHome";
        emit(onHome());
    }
}

void DbbusQmlApp::hide() {
    qDebug()<<"hidding QML APP";
    if(!modeComposite){
        qmlViewer->showMinimized();
    }else{
        backgroundWidget->showMinimized();
    }
    emit(hided());
}






















/*qDebug()<<"back geo"<< backgroundWidget->geometry();
qDebug()<<"back frameSize"<< backgroundWidget->frameSize();
qDebug()<<"back frameGeometry"<< backgroundWidget->frameGeometry();
qDebug()<<"top geo"<< qmlViewer->geometry();*/

/*
connect(qmlViewer,SIGNAL(focusOutEventSglRedirect()),this,SLOT(hideTop()));
connect(backgroundWidget,SIGNAL(focusInEventSglRedirect()),this,SLOT(showTop()));
connect(backgroundWidget,SIGNAL(focusOutEventSglRedirect()),this,SLOT(hideTop()));
*/


/*
QFlag f = !Qt::X11BypassWindowManagerHint;
qmlViewer->setWindowFlags(f);
*/

/*qmlViewer->setWindowModality(Qt::ApplicationModal);
qmlViewer->setWindowFlags(Qt::X11BypassWindowManagerHint);
qmlViewer->show();
qmlViewer->raise();
qmlViewer->setFocus();*/


//qmlViewer->setWindowModality(Qt::ApplicationModal);
//qmlViewer->setWindowFlags(Qt::FramelessWindowHint);
